<?php

declare(strict_types=1);

namespace app\common;

use think\model;
use think\Validate;

class Curd
{
    /**
     * @var Model
     */
    protected $model = null;

    /**
     * @var Validate
     */
    protected $Validate = null;

    /**
     * 查询 function
     *
     * @return void
     */
    public function query()
    {
        if (request()->isPost()) {
            $param = request()->param();
            $order = [];
            if (isset($param["order"]) && $param["order"]["value"] != "") {
                $order[$param["order"]["key"]] = $param["order"]["value"];
            }
            $list = $this->model::where(function ($query) use ($param) {
                // 条件查询
                if (isset($param["where"])) {
                    $query->withSearch(array_keys($param["where"]), $param["where"]);
                }
            })->order($order)
                ->paginate([
                    "page" => $param["page"],
                    "list_rows" => $param["limit"]
                ]);
            return json([
                "code" => 0,
                "msg" => "ok",
                "count" => $list->total(),
                "data" => $list->getCollection()
            ]);
        }
        return view();
    }

    /**
     * 插入 function
     *
     * @return void
     */
    public function create()
    {
        if (request()->isPost()) {
            $param = request()->param();
            if ($this->Validate != null) {
                try {
                    validate($this->Validate::class)
                        ->scene('create')
                        ->check($param);
                } catch (\think\exception\ValidateException $e) {
                    return json([
                        "code" => 2,
                        "msg" => $e->getError(),
                        "data" => $e
                    ]);
                }
            }
            $res = $this->model::create($param);
            return json([
                "code" => 0,
                "msg" => "操作成功",
                "data" => $res
            ]);
        }
        return view();
    }

    /**
     * 更新 function
     *
     * @return void
     */
    public function update()
    {
        $id = request()->param('id');
        $find = $this->model::find($id);
        if (request()->isPost()) {
            $param = request()->param();
            if ($this->Validate != null) {
                try {
                    validate($this->Validate::class)
                        ->scene('update')
                        ->check($param);
                } catch (\think\exception\ValidateException $e) {
                    return json([
                        "code" => 2,
                        "msg" => $e->getError(),
                        "data" => $e
                    ]);
                }
            }
            $res = $this->model::update($param);
            return json([
                "code" => 0,
                "msg" => "操作成功",
                "data" => $res
            ]);
        }
        return view(vars: ["find" => $find]);
    }

    /**
     * 删除 function
     *
     * @return void
     */
    public function delete()
    {
        $id = request()->param("id");
        $this->model::destroy($id);
        return json(["code" => 0, "msg" => "操作成功", "data" => ""]);
    }
}
